% nctoolbox_info  Provide information on the NCTOOLBOX version and dependencies
%

% David Forrest <drf@vims.edu>
% 2015-03-12

% Edited by Brian Schlining 2015-03-12

%% Extract paths
fpath = fileparts(mfilename('fullpath'));
rootpath = fileparts(fpath);
gitpath = fullfile(rootpath, '.git');
javapath = fullfile(rootpath, 'java');

%% Display matlab and toolbox versions
ver % Other toolboxes include jars that we may need to know about
%ver('nctoolbox')
fprintf(1, '\n');


%% Display java jars
fprintf(1, 'Java jar files included with nctoolbox:\n');
jars = dir(fullfile(javapath, '*.jar'));
for i = 1:length(jars)
    fprintf(1, '\t%s\n', fullfile(fpath, jars(i).name));
end
fprintf(1, '\n');

%% Display java class files
fprintf(1, 'Java class files included with nctoolbox:\n');
jars = dir(fullfile(javapath, '*.class'));
for i = 1:length(jars)
    fprintf(1, '\t%s\n', fullfile(fpath, jars(i).name));
end
fprintf(1, '\n');

%% Check to see if git is installed
[rv, r] = system('git --version');
useGit = exist(gitpath,'dir') && ~rv;
%useGit = false; % for testing without git

%% Display git info. Use git if we can, otherwise try to use file generated by git hook
if useGit
  % git information
  try
    fprintf(1, 'nctoolbox revision in %s:\n', rootpath);
    [rv, ncDescribe] = system(sprintf('cd %s; git describe --tags', rootpath));
    [rv, ncCommit] = system(sprintf('cd %s; git rev-parse --short HEAD', rootpath));
    % git log behaves weirdly with old git versions, like the one shipped by Apple.
    % Disabled date for now as git log is quirky between versions
    %[rv, ncDate] = system(sprintf('cd %s;git --no-pager log -1 --format=%cd --date=short', rootpath));
    [rv, ncBranch] = system(sprintf('cd %s;git rev-parse --abbrev-ref HEAD', rootpath));
    [rv, ncDate] = system(sprintf('cd %s;git --no-pager log -1 --format=%cd --date=short', rootpath));
    fprintf(1, 'GIT COMMIT:   %s', ncCommit);
    fprintf(1, 'GIT BRANCH:   %s', ncBranch);
    %fprintf(1, 'GIT DATE:     %s', ncDate);
    fprintf(1, 'GIT DESCRIBE: %s', ncDescribe);
  catch
      useGit = false;
  end
end

if ~useGit
    fprintf(1, 'last known nctoolbox revision in %s:\n', rootpath);
    infofile = fullfile(fpath, 'nctoolbox-version.txt');
    if exist(infofile, 'file')
        type(infofile);
    else
        fprintf(1, 'No GIT revision information is available\n');
    end
end

fprintf(1, '\n');
